package s.k.gui.webtest;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import org.openqa.selenium.WebDriver;

import s.k.gui.webtest.BrowserWindow;
import s.k.gui.webtest.basetools.SKBaseTools;

public class Allocator {

	private List<BrowserWindow> windows;

	private String screenShotDir;

	public String getScreenShotDir() {

		return screenShotDir;
	}

	public List<BrowserWindow> getWindows() {

		return windows;

	}

	/*
	 * 
	 *ַ
	 */
	private volatile static Allocator allocator;

	public BrowserWindow currentWindow;

	private Allocator() {

		screenShotDir = SKBaseTools.getScreenShotDirPath();

		SKBaseTools.configScreenShotLocation();

		this.windows = new ArrayList<BrowserWindow>();

	}

	/*
	 * 
	 * 
	 */
	public static Allocator getInstance() {

		if (allocator == null) {

			synchronized (Allocator.class) {

				if (allocator == null) {

					allocator = new Allocator();

				}

			}
		}
		return allocator;
	}

	/*
	 * 
	 */
	public void destroy() {

		this.currentWindow.close();

		closeAllWindows();

	}

	public void closeAllWindows() {

		updateWindows();

		for (BrowserWindow window : windows) {

			window.close();
		}
		windows.clear();
	}

	public void closeWindowByUrl(String url) {

		for (BrowserWindow window : windows) {

			if (window.getUrl().equalsIgnoreCase(url)) {

				window.close();

				windows.remove(window);

				if (currentWindow.equals(window)) {

					if (windows.size() == 0) {

						this.currentWindow = null;

					} else {

						this.currentWindow = this.windows.get(0);
					}
				}

			} else {

				System.out.println("没有找到要关闭的网页！");
			}
		}
	}

	public void closeAllWindowsByUrl(String url) {

		closeWindowByUrl(url);

	}

	public void updateWindows() {

		int i = 0;

		for (String handle : ((WebDriver) currentWindow.getDriver().getEngine())
				.getWindowHandles()) {

			for (; i < windows.size(); i++) {

				if (handle.equalsIgnoreCase(windows.get(i).getHandle())) {

					break;
				}
			}
			if (i >= windows.size()) {

				WebDriverBrowserWindow webDriverBrowserWindow = new WebDriverBrowserWindow(
						currentWindow.getDriver());

				webDriverBrowserWindow.setWindowHandle(handle);

				windows.add(webDriverBrowserWindow);

			}
		}
	}

	/**获取地址为url的句柄 */
	public void attach(String url) {

		for (BrowserWindow window : windows) {

			if (window.getUrl().equalsIgnoreCase(url)) {

				window.focus();

				break;
			}
		}
	}

	public void attachByContains(String url) {

		for (BrowserWindow window : windows) {

			if (window.getUrl().contains(url)) {

				window.focus();
				break;

			}
		}
	}

	/**通过正则表达式来获取url的句柄*/
	public void attachByRegular(String url) {

		Pattern pattern = Pattern.compile(url);

		for (BrowserWindow window : windows) {

			if (pattern.matcher(window.getUrl()).matches()) {

				window.focus();

				break;
			}
		}
	}

	/**
	 *默认增加window为IE<br>
	 * 
	 */
	public void addBrowserWindow() {

		addWebDriverIEBrowserWindow();

	}

	/**
	 * 增加指定引擎的window
	 */
	public void addBrowserWindow(BrowserType browserType) {

		switch (browserType) {

		case WebDriverAndroid:

		case WebDriverChrome:

		case WebDriverFirefox:

		case WebDriverSafari:

		case WebDriverIE:

		case WebDriverIphone:

		case HtmlUnit:

			addWebDriverBrowserWindow(browserType);

			// open(url);

			break;
		}
	}

	public void addWebDriverIEBrowserWindow() {

		WebDriverBrowserWindow window = new WebDriverBrowserWindow(
				SeleniumDriver.createWebDriverIEEngine());

		this.windows.add((BrowserWindow) window);

	}

	public void addWebDriverFirefoxBrowserWindow() {

		WebDriverBrowserWindow window = new WebDriverBrowserWindow(
				SeleniumDriver.createWebDriverFireFoxEngine());

		this.windows.add(window);

	}

	
	public void addWebDriverChromeBrowserWindow() {

		WebDriverBrowserWindow window = new WebDriverBrowserWindow(
				SeleniumDriver.createWebDriverChromeEngine());

		this.windows.add(window);
	}


	public void addWebDriverSafariBrowserWindow() {

		WebDriverBrowserWindow window = new WebDriverBrowserWindow(
				SeleniumDriver.createWebDriverSafariEngine());

		this.windows.add(window);

	}

	
	public void addWebDriverIphoneBrowserWindow() {

		WebDriverBrowserWindow window = new WebDriverBrowserWindow(
				SeleniumDriver.createWebDriverIphoneEngine());

		this.windows.add(window);
	}

	public void addWebDriverAndroidBrowserWindow() {

		WebDriverBrowserWindow window = new WebDriverBrowserWindow(
				SeleniumDriver.createWebDriverAndroidEngine());

		this.windows.add(window);
	}


	private void addHtmlUnitDriverBrowserWindow() {

		WebDriverBrowserWindow window = new WebDriverBrowserWindow(
				SeleniumDriver.createWebDriverHtmlUnitEngine());

		this.windows.add(window);

	}

	public void addWebDriverBrowserWindow(BrowserType browserType) {

		switch (browserType) {

		case WebDriverIE:

			addWebDriverIEBrowserWindow();

			break;

		case WebDriverFirefox:

			addWebDriverFirefoxBrowserWindow();

			break;

		case WebDriverChrome:

			addWebDriverChromeBrowserWindow();

			break;

		case WebDriverSafari:

			addWebDriverSafariBrowserWindow();

		case WebDriverIphone:

			addWebDriverIphoneBrowserWindow();

			break;

		case WebDriverAndroid:

			addWebDriverAndroidBrowserWindow();

			break;

		case HtmlUnit:

			addHtmlUnitDriverBrowserWindow();

		default:

			break;

		}
	}



	public void addAndSetBrowserWindow(BrowserType browserType) {

		addBrowserWindow(browserType);

		currentWindow = windows.get(windows.size() - 1);
	}

}
